import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.*;
import java.util.ArrayList;

public class Serveur {

	public static void main(String[] zero){
		ServerSocket socket;
		
		try {
			socket = new ServerSocket(2009);
			Thread t = new Thread(new Accepter_clients(socket));
			t.start();
		}
		catch (IOException e) {
			e.printStackTrace();
		}
	}
}

class Accepter_clients implements Runnable {

	private ServerSocket socketserver;
	private Socket socket;
	private int nbrclient = 0;
	public Accepter_clients(ServerSocket s){
		socketserver = s;
	}
		
	public void run() {
		BufferedReader in;
		PrintWriter out;
		
		try {
			while(true){
				System.out.println("serveur en attente de connexions clientes");
	       		socket = socketserver.accept(); // Un client se connecte, on l'accepte
	       		nbrclient++;
	       		System.out.println(+nbrclient);
	       		out = new PrintWriter(socket.getOutputStream());
		  		out.println(requete(1));
		  		out.flush();
				in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
				String message_distant = in.readLine();
				System.out.println(message_distant);
		  		socket.close();
	       	}
		} catch (IOException e) {e.printStackTrace();}
	}
		
	public String requete(int type_requete) {
		String buffer0 = "IP";
		String buffer1 = "PORT";
		String buffer2 = "DEMANDE DE COUPE";
		String buffer3 = "DEMANDE DE DECISION";
		String buffer4 = "DEMANDE DE DECISION - PROPOSITION";
		String buffer5 = "DEMANDE JOUER-CARTE";
		String buffer6 = "SCORE TEMPORAIRE";
		String buffer7 = "SCORE DEFINITIF";
			
		String requete = new String();
		switch (type_requete) {
			case 1:		// Distribution, demande de coupe
				requete = "ENSIBELOTE  "+buffer0+"  "+buffer1+"  "+buffer2;
				break;
			case 2:		// Demande prise de decision
				requete = "ENSIBELOTE  "+buffer0+"  "+buffer1+"  "+buffer3;
				break;
			case 3:		// Demande prise de decision proposition
				requete = "ENSIBELOTE  "+buffer0+"  "+buffer1+"  "+buffer4;				
				break;
			case 4:		// Jouer carte
				requete = "ENSIBELOTE  "+buffer0+"  "+buffer1+"  "+buffer5;
				break;
			case 5:		// Affichage score
				requete = "ENSIBELOTE  "+buffer0+"  "+buffer1+"  "+buffer6;
				break;
			default:	// RIEN
				requete = "ENSIBELOTE  null";
				break;
		}
		return requete;
	}
}